/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React,{useEffect,useState} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
    Button,
  StatusBar,
    NativeModules,
    PermissionsAndroid
} from 'react-native';
import BleUtil from './native_modules/BleUtil';
import ToastUtil from './native_modules/ToastUtil';
import CartSkin from './CartSkin';
import Scan from './Scan';
import WifiManager from "react-native-wifi-reborn";

const requestPermission = async () => {
    try {
        const result = await PermissionsAndroid.requestMultiple([
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
            PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
            ]
        );
        // console.log("request permission result",result)

    } catch (err) {
        console.warn(err);
    }
};

const App = () => {

    const [page,setPage] = useState(0)
    const [statusText,setStatusText] = useState('准备就绪')
    //是否是拍照
    const [isScan,setIsScan] = useState(true)
    const [wifiname,setwifiname] = useState("wifiname")
    useEffect(()=>{
        //初始化

        requestPermission().then(r => {

        })

        BleUtil.init((data)=>{
            console.log(data)
            const {event} = data
            if(event === 'startScanBle'){
                setStatusText('正在扫描蓝牙')
            }else if(event === 'MSG_OPEN_WIFI_SUCCESS'){
                setStatusText('已打开设备WiFi')
            }else if(event === 'MSG_WIFI_CONNECTED'){
                setStatusText('WIFI已连接')
                if(isScan){
                    setPage(1)
                }else{
                    setPage(2)
                }
            } else if(event === 'MSG_WIFI_DISCONNECTED'){
                setStatusText('WIFI已断开')
            }else if(event === 'NoDeviceFind'){
                setStatusText('找不到设备')
            }else if(event === 'ConnectDeviceWifiTimeOut'){
                setStatusText('连接WiFi超时')
            }else if(event === 'MSG_AUTO_CONNECT_FAILED'){
                setStatusText('自动连接失败')
            }else if(event === 'getSSID'){

                //这里获取ssid
                console.log(data.ssid)
                setwifiname(data.ssid)

            }

        })

    },[])




    const renderPage = ()=>{
        if(page === 0){
            return (
                <View style={styles.container}>

                    <Text>{statusText}</Text>

                    <View style={{height:20}}/>

                    <Button
                        title={'测皮肤拍照'}
                        onPress={()=>{
                            setIsScan(true)
                            BleUtil.startScan(true).then(res=>{
                                console.log('测肤',res)
                            }).catch(error=>{
                                console.log('测肤 error',error)
                            })
                        }}
                    />

                    <View style={{height:20}}/>
                    
                    <Button
                        title={'wifi'}
                        onPress={()=>{
                            WifiManager.connectToProtectedSSID(wifiname, "", false).then(
                                () => {
                                  console.log("Connected successfully!");
                                },
                                () => {
                                  console.log("Connection failed!");
                                }
                            );

                        }}
                    />
                    <Button
                        title={'美肤'}
                        onPress={()=>{

                            BleUtil.startScan().then(res=>{
                                console.log('美肤',res)
                            }).catch(error=>{
                                console.log('美肤 error',error)
                            })
                        }}
                    />
                </View>
            )
        }

        if(page == 1){
            return (
                <Scan/>
            )
        }

        if(page == 2){
            return (
                <CartSkin/>
            )
        }
    }


  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView  style={{flex:1}}>

          {renderPage()}

          <Button
              title={'重置'}
              onPress={()=>{
                  setStatusText('准备就绪')
                  setPage(0)
              }}
          />

      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({

  container:{
    flex:1,
    justifyContent:'center',
    alignItems:'center'
  },
  btn:{

  }
});

export default App;
